//带傀儡节点的双向链表
class puppetNode{
    public int val;
    public puppetNode prev;
    public puppetNode next;

    public puppetNode(int val){
        this.val = val;
    }
}

public class PuppeDLL {
    puppetNode puphead;
    public puppetNode last;

    //创建一个构造方法，在实例化同时就会生成傀儡节点
    //因此傀儡链表不会有空的情况
    public PuppeDLL(){
        this.puphead = new puppetNode(-1);
    }

    public int size() {
        int count = 0;
        puppetNode cur = this.puphead.next;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        if (puphead == null){
            System.out.println("链表为空");
            return;
        }
        puppetNode cur= this.puphead.next;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data) {
        puppetNode node = new puppetNode(data);
        puppetNode cur= this.puphead.next;
        if (cur == null){
            this.puphead.next = node;
            node.prev = this.puphead;
            this.last = node;
        }else {
            cur.prev = node;
            node.prev = puphead;
            node.next = cur;
            puphead.next = node;
        }

    }

    //尾插法
    public void addLast(int data) {
        puppetNode node = new puppetNode(data);
        //别忘了链表除傀儡的情况
        if (puphead.next == null && this.last == null){
            this.puphead.next = node;
            node.prev = this.puphead;
            this.last = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public puppetNode searchIndex(int index){
        puppetNode cur = this.puphead.next;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()){
            return;
        }else {
            if (index == 0){
                addFirst(data);
                return;
            }else if (index == size()){
                addLast(data);
                return;
            }else {
                puppetNode node = new puppetNode(data);
                puppetNode cur = searchIndex(index);
                node.prev = cur.prev;
                node.next = cur;
                cur.prev.next = node;
                cur.prev = node;
            }
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        puppetNode cur = this.puphead.next;
        while (cur != null){
            if (key == cur.val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        puppetNode cur = this.puphead.next;
        while (cur != null){
            if (cur.val == key){
                if (cur == this.puphead.next){
                    puphead.next = cur.next;
                    if (cur.next != null){
                        cur.next.prev = null;
                    }else {
                        this.last = null;
                    }
                }else if(cur == this.last){
                    cur.prev.next = null;
                    this.last = cur.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        puppetNode cur = this.puphead.next;
        while (cur != null){
            if (cur.val == key){
                if (cur == this.puphead.next){
                    puphead.next = cur.next;
                    if (cur.next != null){
                        cur.next.prev = null;
                    }else {
                        this.last = null;
                    }
                }else if(cur == this.last){
                    cur.prev.next = null;
                    this.last = cur.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    public void clear() {
        puppetNode cur = this.puphead.next;
        puppetNode curNext;
        while (cur != null){
            //定义在循环内，不然curNext就赋值一次
            curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.puphead = null;
        this.last = null;
    }

}
